/* *
 * @program: LivePublisher
 *
 * @description: ${description}
 *
 * @author: 909845
 *
 * @create: 2019-03-28 15:49
***/
#define OVIDEOCDNCLIENTAPI_EXPORT
#include "MediaRemux.h"
#include "../Media/FlvRemux.h"
#include "../Media/TsRemux.h"
#include "../Public/WeakCallback.h"

#define TAG "SID[" << m_strStreamID << "]- "

CMediaRemux::CMediaRemux(const char *pStreamID, eLivePublisherStreamType eType):
                  m_strStreamID(pStreamID)
{
    TRACE_LOG(TAG << "Create CMediaRemux");
    if (eLivePublish_FLV == eType)
    {
        m_spRemux = shared_ptr<CFlvRemux>(new CFlvRemux(m_strStreamID));
    }
    else
    {
        m_spRemux = shared_ptr<CTsRemux>(new CTsRemux(m_strStreamID));
    }
}

CMediaRemux::~CMediaRemux()
{
    TRACE_LOG(TAG << "Destroy ~CMediaRemux");
}

bool CMediaRemux::Initialize(bool bHaveAudio, void *pContext, MediaRemuxCb callback)
{
    bool bRet = false;
    do
    {
        Assert(pContext != nullptr);
        Assert(callback != nullptr);
        auto lambaRemuxStreamCb = [pContext, callback](const char* pData, int nLen)
        {
            callback(pContext, pData, nLen);
        };
        if (!m_spRemux->Initlize(std::bind(lambaRemuxStreamCb, placeholders::_1, placeholders::_2), bHaveAudio))
        {
            ERROR_LOG(TAG << "m_spRemux Initlize failed.");
            break;
        }
        bRet = true;
    } while (0);

    return bRet;
}

void CMediaRemux::PushH264(const char *pData, int nLen, unsigned int uTimestamp)
{
    m_spRemux->RecvVideoStream(pData, nLen, uTimestamp);
}

void CMediaRemux::PushAac(const char *pData, int nLen, unsigned int uTimestamp)
{
    m_spRemux->RecvAudioStream(pData, nLen, uTimestamp);
}

void CMediaRemux::Release()
{
    delete this;
}

IMediaRemux* CreateMediaRemux(const char *pStreamID, eLivePublisherStreamType eType)
{
    return new CMediaRemux(pStreamID, eType);
}
